/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.web;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.modules.storage.entity.Category;
import com.jeesite.modules.storage.service.BrandService;
import com.jeesite.modules.storage.service.CategoryService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 分类Controller
 */
@Controller(value = "webcategory")
@RequestMapping(value = "${adminPath}/storage/category")
public class CategoryController extends BaseController {

	@Autowired
	private CategoryService categoryService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Category get(String id, boolean isNewRecord) {
		return categoryService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(Category category, Model model) {
		model.addAttribute("category", category);
		return "modules/storage/category/list";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Category> listData(Category category, HttpServletRequest request, HttpServletResponse response) {
		category.setPage(new Page<>(request, response));
		Page<Category> page = categoryService.findPage(category);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(Category category, Model model) {
		model.addAttribute("category", category);
		return "modules/storage/category/form";
	}

	/**
	 * 保存分类
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Category category) {
		try{
			categoryService.save(category);
			return renderResult(Global.TRUE, text("保存分类成功！"));
		}catch (Exception ex){
			return renderResult(Global.FALSE, text(ex.getMessage()));
		}
	}
	
	/**
	 * 删除分类
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Category category) {
		try{
			categoryService.delete(category);
			return renderResult(Global.TRUE, text("删除分类成功！"));
		}catch (Exception ex){
			return renderResult(Global.FALSE, text(ex.getMessage()));
		}
	}

	/**
	 * 导出数据
	 */
	@RequestMapping(value = "exportData")
	public void exportData(HttpServletResponse response) {

	}

	/**
	 * 下载导入数据模板
	 */
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletResponse response) {
		importTemplate(response,"分类 - 大仓库.xlsx");
	}

	/*
	* 读取Excel到列表
	*/
	private List<Category> readExcelToList(MultipartFile file){
		try {
			List<Category> categories = new ArrayList<>();
			Workbook workbook = getWorkbook(file);
			Sheet sheet = workbook.getSheetAt(0);
			for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
				Row row = sheet.getRow(rowNum);
				if (row == null  || row.getLastCellNum() < 2) {
					continue;
				}
				String sortStr = getCellValue(row,0);
				if(StringUtils.isBlank(sortStr)){
					continue;
				}
				Category category = new Category();
				category.setSort(Integer.parseInt(sortStr));
				category.setName(getCellValue(row,1));
				categories.add(category);
			}
			return categories;
		} catch (Exception ex) {
			throw ex;
		}
	}

	/*
	 * 读取Excel到列表
	 */
	private String check(List<Category> categories){
		return "";
	}

	/**
	 * 导入数据
	 */
	@ResponseBody
	@PostMapping(value = "importData")
	public String importData(MultipartFile file) {
		try {
			List<Category> categories = readExcelToList(file);
			String msg = check(categories);
			if(StringUtils.isNotBlank(msg)){
				return renderResult(Global.FALSE, "导入出错:" + msg);
			}
			for (Category category : categories){
				category.setId(categoryService.getIdByName(category.getName()));
				categoryService.save(category);
			}
			return renderResult(Global.TRUE,"导入成功");
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "导入出错:" + ex.getMessage());
		}
	}
}